package com.example.leetdemo.utils;

import lombok.extern.slf4j.Slf4j;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;


/**
 * @title: DateUtils.java
 * @description: 日期工具类
 * @author: xuexumin
 * @date: 2021-3-24 17:29:18
 */
@Slf4j
public class DateUtils {

    private static final String YEAR = "yyyy";
    private static final String MONTH = "yyyy-MM";
    private static final String MONTH2 = "yyyyMM";
    private static final String DAY = "yyyy-MM-dd";
    private static final String MONTH_DAY = "MMdd";
    private static final String HOUR_MINUTE = "HH:mm";
    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(LocalDateTime localDateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_PATTERN);
        return localDateTime.format(formatter);
    }
    public static String format(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    public static LocalDateTime parse(String str) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_PATTERN);
        return LocalDateTime.parse(str, formatter);
    }

    public static LocalDateTime parse(String str, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(str, formatter);
    }

    public static LocalDate parseDay(String str) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DAY);
        return LocalDate.parse(str, formatter);
    }

    public static YearMonth parseMonth(String str) {
        return YearMonth.parse(str);
    }

    public static Year parseYear(String str) {
        return Year.parse(str);
    }
    /**
     * @method: localDateTimeToMilli
     * @description: LocalDateTime转毫秒数
     * @param: localDateTime
     * @return: long
     * @author: xuexumin
     * @date: 2021-3-24 17:30:08
     */
    public static long localDateTimeToMilli(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }


    /**
     * 获取当前年
     *
     * @return 当前年
     */
    public static String getCurrentYearRecord() {
        return DateTimeFormatter.ofPattern(YEAR).format(LocalDateTime.now());
    }

    /**
     * 获取去年
     * @return
     */
    public static String getLastYearRecord() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(LocalDateTime.now().getYear() - 1);
        return buffer.toString();
    }
    /**
     * 获取明年
     * @return
     */
    public static String getNextYearRecord() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(LocalDateTime.now().getYear() + 1);
        return buffer.toString();
    }
    /**
     * 获取当前年月
     *
     * @return 当前年月
     */
    public static String getCurrentMonthRecord() {
        return DateTimeFormatter.ofPattern(MONTH).format(LocalDateTime.now());
    }
    public static String getCurrentMonthRecord2() {
        return DateTimeFormatter.ofPattern(MONTH2).format(LocalDateTime.now());
    }
    public static String getLastMonthRecord2() {
        return DateTimeFormatter.ofPattern(MONTH2).format(LocalDateTime.now().minusMonths(1));
    }

    public static String getLatestMonthRecord() {
        return DateTimeFormatter.ofPattern(MONTH).format(LocalDateTime.now().plusMonths(-1));
    }
    public static String getNextMonthRecord() {
        return DateTimeFormatter.ofPattern(MONTH).format(LocalDateTime.now().plusMonths(1));
    }

    /**
     * 获取上月年月
     *
     * @return 上月年月
     */
    public static String getLastMonthRecord() {
        return DateTimeFormatter.ofPattern(MONTH).format(LocalDateTime.now().minusMonths(1));
    }
    /**
     * 获取上个月的年份
     *
     * @return 上个月的年份
     */
    public static String getLastMonthYearRecord() {
        return DateTimeFormatter.ofPattern(YEAR).format(LocalDateTime.now().minusMonths(1));
    }
    /**
     * 将毫秒时间戳转LocalDateTime
     * @param milliseconds
     * @return
     */
    public static LocalDateTime msToLocalDateTime(Long milliseconds) {
        try {
            if (milliseconds == null) {
                return null;
            }
            return LocalDateTime.ofEpochSecond(milliseconds / 1000, 0, ZoneOffset.ofHours(8));
        } catch (Exception e) {
            log.error("将毫秒时间戳转LocalDateTime异常>>>{}", e);
            return null;
        }
    }

    /**
     * 将秒时间戳转LocalDateTime
     * @param seconds
     * @return
     */
    public static LocalDateTime sToLocalDateTime(long seconds) {
        try {
            return LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.ofHours(8));
        } catch (NullPointerException e) {
            log.error("将秒时间戳转LocalDateTime异常>>>{}", e);
            return null;
        }
    }

    /**
     * 获取当前年月日
     *
     * @return 当前年月日
     */
    public static String getCurrentDayRecord() {
        return DateTimeFormatter.ofPattern(DAY).format(LocalDateTime.now());
    }

    /**
     * 获取昨天年月日
     *
     * @return 昨天年月日
     */
    public static String getYesterdayDayRecord() {
        return DateTimeFormatter.ofPattern(DAY).format(LocalDateTime.now().minusDays(1));
    }
    /**
     * 获取昨天年月
     *
     * @return 昨天年月
     */
    public static String getYesterdayMonthRecord() {
        return DateTimeFormatter.ofPattern(MONTH2).format(LocalDateTime.now().minusDays(1));
    }

//    public static String getQueryYear(String year) {
//        String result;
//        if (StringUtils.isNotBlank(year)) {
//            result = year.substring(0, 4);
//        } else {
//            result = getCurrentYearRecord();
//        }
//        return result;
//    }

    /**
     * 获取当前月的第一天
     *
     * @return 当前月第一天
     */
    public static String getCurrentMonthFirstDay() {
        return DateTimeFormatter.ofPattern(DAY).format(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()));
    }

    /**
     * 获取当年的第一天
     *
     * @return 当前年第一天
     */
    public static String getCurrentYearFirstDay() {
        return DateTimeFormatter.ofPattern(DAY).format(LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear()));
    }

    /**
     * 获取当前月日
     *
     * @return 当前月日
     */
    public static String getCurrentMonthDayText() {
        String monthDay = DateTimeFormatter.ofPattern(MONTH_DAY).format(LocalDateTime.now());
        return monthDay.substring(0, 2) + "月" + monthDay.substring(2, monthDay.length()) + "日";
    }

    /**
     * 获取上月的第一天
     *
     * @return 上月的第一天
     */
    public static String getLastMonthFirstDay() {
        return DateTimeFormatter.ofPattern(DAY).format(LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()));
    }

    /**
     * 获取本周一的日期
     *
     * @return 本周一的日期
     */
    public static String getCurrentWeekMonday() {
        return DateTimeFormatter.ofPattern(DAY).format(LocalDate.now().with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1));
    }

    /**
     * 获取上周一的日期
     *
     * @return 上周一的日期
     */
    public static String getLastWeekMonday() {
        return DateTimeFormatter.ofPattern(DAY).format(LocalDate.now().with(TemporalAdjusters.
                previous(DayOfWeek.SUNDAY)).plusDays(1).minusDays(7));
    }

    /**
     * 获取当前时间的时分
     *
     * @return 当前时间的时分
     */
    public static String getCurrentHourMinute() {
        return DateTimeFormatter.ofPattern(HOUR_MINUTE).format(LocalDateTime.now());
    }

    /**
     * 指定的日期减少N天
     *
     * @param dayRecord 指定的日期
     * @param days 减少的天
     * @return 减少后的日期
     */
    public static String getAssignedDayMinus(String dayRecord, Integer days) {
        return LocalDate.parse(dayRecord).minusDays(days).toString();
    }

    /**
     * 指定的日期增加N天
     *
     * @param dayRecord 指定的日期
     * @param days 增加的天
     * @return 增加后的日期
     */
    public static String getAssignedDayPlus(String dayRecord, Integer days) {
        return LocalDate.parse(dayRecord).plusDays(days).toString();
    }

    /**
     * 指定的月份减少N月
     *
     * @param monthRecord 指定的月份
     * @param months 减少的天
     * @return 减少后的月份
     */
    public static String getAssignedMonthMinus(String monthRecord, Integer months) {
        return YearMonth.parse(monthRecord).minusMonths(months).toString();
    }

}
